package com.tmt.im.manager.controller.api;

import com.tmt.im.manager.controller.vo.UploadFileRequest;
import com.tmt.im.manager.service.FileService;
import com.tmt.springboot.common.restful.ExecuteResult;
import com.tmt.springboot.common.restful.IExecuteResult;
import com.tmt.springboot.common.restful.ResultWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.scheduling.annotation.Async;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.concurrent.CompletableFuture;

/**
 * 用户头像控制器
 *
 * @Description TODO
 * @Author wuyi
 * @Date 2024/8/25 11:18
 * @Version 1.0
 **/
@Async("asyncTaskExecutor")
@Tag(name = "文件上传下载管理", description = "用户头像、群组头像、其他文件等上传下载相关接口")
@RestController
@RequestMapping("${api-url-prefix}/file")
public class FileController {
    private final FileService fileService;

    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @Operation(summary = "上传文件接口",
            description = "上传用户头像、群组头像、或其他各类文件",
            operationId = "uploadFile",
            parameters = {
                    @Parameter(name = "request", required = true, description = "上传文件请求对象")
            })
    @PostMapping(value = "/upload")
    public CompletableFuture<IExecuteResult<ResultWrapper<String>>> uploadFile(@Validated UploadFileRequest request,
                                                                               Principal principal) {
        if (request.getType() == 1 && request.getTid() == null) {
            return CompletableFuture.completedFuture(ExecuteResult.failure("上传群组头像时，群组ID参数不能为null", null));
        }
        IExecuteResult<String> result = fileService.uploadFile(request, principal);
        return CompletableFuture.completedFuture(ResultWrapper.from(result));
    }

    @Operation(summary = "获取用户头像图片接口",
            description = "获取用户默认头像图片",
            operationId = "getUserAvatar")
    @GetMapping(value = "/avatar/user/{userId:.+}")
    public CompletableFuture<Void> getUserAvatar(@PathVariable("userId") String strUserId,
                                                 HttpServletResponse response) {
        String[] infos = strUserId.split("\\.");
        Long userId = Long.parseLong(infos[0]);
        String ext = infos.length > 1 ? infos[1] : null;
        fileService.downloadFile(0, userId, ext, response);
        return CompletableFuture.completedFuture(null);
    }

    @Operation(summary = "获取聊天群组头像图片接口",
            description = "获取聊天群组头像图片接口",
            operationId = "getGroupAvatar")
    @GetMapping(value = "/avatar/group/{groupId:.+}")
    public CompletableFuture<Void> getGroupAvatar(@PathVariable("groupId") String strGroupId,
                                                  HttpServletResponse response) {
        String[] infos = strGroupId.split("\\.");
        Long groupId = Long.parseLong(infos[0]);
        String ext = infos.length > 1 ? infos[1] : null;
        fileService.downloadFile(1, groupId, ext, response);
        return CompletableFuture.completedFuture(null);
    }

    @Operation(summary = "下载文件接口",
            description = "下载文件接口",
            operationId = "downloadFile")
    @GetMapping(value = "/download/{fileId:.+}")
    public CompletableFuture<Void> downloadFile(@PathVariable("fileId") String strFileId,
                                                HttpServletResponse response) {
        String[] infos = strFileId.split("\\.");
        Long fileId = Long.parseLong(infos[0]);
        String ext = infos.length > 1 ? infos[1] : null;
        fileService.downloadFile(2, fileId, ext, response);
        return CompletableFuture.completedFuture(null);
    }

}
